package com.meiqiongproperty.yiguanjia.activity;

import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;

import com.meiqiongproperty.yiguanjia.R;
import com.meiqiongproperty.yiguanjia.commom.ViewConstant;
import com.meiqiongproperty.yiguanjia.commom.YApplication;
import com.meiqiongproperty.yiguanjia.db.utils.DBUserUtils;
import com.meiqiongproperty.yiguanjia.http.api.ApiService;
import com.meiqiongproperty.yiguanjia.http.interfaces.IHttpCallback;
import com.meiqiongproperty.yiguanjia.http.sendJsonModel.Advertising;
import com.meiqiongproperty.yiguanjia.httpbean.HomeNoticeBean;
import com.meiqiongproperty.yiguanjia.utils.Utils;

import java.util.List;

/**
 * 起始页
 *
 * @author Chen
 * @version V1.0
 * @package com.meiqiongproperty.cnotes.activity
 * @date 17/1/12
 */
public class SplashActivity extends BaseFragmentActivity {
    private boolean isStartHome = false;
    // 应用外跳入
    private boolean isFromOutApp = false;
    private Context mContext;
    private SharedPreferences mSharedPreferences;
    private static final String VERSION_CODE = "version_code";
    private static final int MSG_DEFAULT_DISPLAY = 0x10001;
    private ImageView mBootupImageView;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_DEFAULT_DISPLAY:
                    mHandler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            nextStep();
                        }
                    }, 3000);

                    break;
            }
        }

    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.boot_up);
        super.onCreate(savedInstanceState);
        mBootupImageView = (ImageView) findViewById(R.id.bootup_image);
        init();
        mHandler.sendEmptyMessage(MSG_DEFAULT_DISPLAY);
    }


    private void init() {
        mContext = YApplication.getInstance().getApplicationContext();
        // 避免通过其他方式启动程序后，再通过程序列表中的launcher启动，重新走启动流程
        if (!isTaskRoot()) {
            Intent intent = getIntent();
            String action = intent.getAction();
            if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && action != null
                    && action.equals(Intent.ACTION_MAIN)) {
                finish();
                return;
            }
        }
        Intent intent = getIntent();
        if (intent != null) {
            if (intent.getExtras() != null) {
                boolean exit = intent.getExtras().getBoolean(
                        ViewConstant.EXIT_APP, false);
                if (exit) {
                    quit();
                }
            }

        } else {
            isFromOutApp = false;
        }
    }

    private void nextStep() {
        if (!DBUserUtils.isLogin(getBaseContext())) {
            Intent intent;
            intent = new Intent(SplashActivity.this,
                    LoginActivity.class);
            startActivity(intent);
            finishCurrentActivity();
        } else {
            Intent intent;
            intent = new Intent(SplashActivity.this,
                    MainActivity.class);
            startActivity(intent);
            finishCurrentActivity();
        }

    }

    // 退出应用
    public void quit() {
        try {
            finish();
            ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
            List<ActivityManager.RunningAppProcessInfo> aps = am.getRunningAppProcesses();
            final String pkgName = getPackageName();
            for (ActivityManager.RunningAppProcessInfo rap : aps) {
                if (rap.processName.startsWith(pkgName)
                        && !rap.processName.endsWith(":push")) {
                    android.os.Process.killProcess(rap.pid);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        if (intent != null && intent.getExtras() != null) {
            boolean exit = intent.getExtras().getBoolean(ViewConstant.EXIT_APP,
                    false);
            isStartHome = intent.getExtras().getBoolean(
                    ViewConstant.START_HOME, false);
            if (exit) {
                quit();
            } else if (isStartHome) {
                Intent mIntent = null;
                mIntent = new Intent(SplashActivity.this,
                        MainActivity.class);
                startActivity(mIntent);

            }
        }
        super.onNewIntent(intent);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (Utils.getToken(this)!=null){
            getNoticeInfo();
        }
    }

    /**
     * 获取公告信息
     */
    public void getNoticeInfo() {
        Advertising advertising = new Advertising();
        advertising.setRoomId(Utils.getRoomID(this));
        advertising.setToken(Utils.getToken(this));
        advertising.setType("1");
        ApiService.getHomeNotice(SplashActivity.this, new IHttpCallback<HomeNoticeBean>() {
            @Override
            public void onNext(HomeNoticeBean homeNoticeBean) {
                ViewConstant.notice.clear();
                List<HomeNoticeBean.NoticeBean> list_notice = homeNoticeBean.getNotice();
                for (int i = 0; i < list_notice.size(); i++) {
                    String title = list_notice.get(i).getTitle();
                    ViewConstant.notice.add(title);
                }
            }

            @Override
            public void onError(int code, String message) {

            }
        }, Utils.getRequestBody(advertising));
    }
}
